/*
 * This is a collection of functions and objects for managing the items and categories view 
 * It use a Object Model we will call the the catalog object model. It is like this:
 * class (object)
 *
 * catalog (myCatalog)
 
  
 * catalog
 * catalog::categoriesList
 * catalog::categoriesList::array(category) (categories)
 * catalog::categoriesList::category[x]::itemsList (itemsList)
 * catalog::categoriesList::category[x]::itemsList:array(smallItem) (items)
 
 * orderCatalog
 * orderCatalog::orderCategoriesList (myCategoriesList)
 * orderCatalog::orderCategoriesList::array(orderCategory) (categories)
 * orderCatalog::orderCategoriesList::orderCategory[x]::orderItemsList (itemsList)
 * orderCatalog::orderCategoriesList::orderCategory[x]::orderItemsList:array(smallItem) (items)
 
 * adminCatalog
 * adminCatalog::adminCategoriesList (myCategoriesList)
 * adminCatalog::adminCategoriesList::array(adminCategory) (categories)
 * adminCatalog::adminCategoriesList::adminCategory[x]::adminItemsList (itemsList)
 * adminCatalog::adminCategoriesList::adminCategory[x]::adminItemsList:array(smallItem) (items)
 */

 
  // myCatalog is the global variable that contain the catalog object model objects. Any time we need to know the catalog
 // state or to modify any data from it we will do it through this variable. We initialize this variable here.

//var myCatalog=new orderCatalog();

function orderCatalogNode(){
	catalogNode.call(this);
	this.myCategory=new orderCategory();
	this.myCategory.parentNode=this;
}

orderCatalogNode.prototype=Object.create(catalogNode.prototype);

orderCatalogNode.prototype.constructor=catalogNode;

orderCatalogNode.prototype.loadCategories=function(caregoriesString) {
	var categoriesPointer=JSON.parse(caregoriesString);
	this.children=new Array();
	// These are root categories
	for (var i in categoriesPointer.categories) {
		this.children[i]=new orderCatalogNode();
		this.children[i].parentNode=this;
		this.children[i].myCategory.setFromArray(categoriesPointer.categories[i]);
	}
}

// This function load the items in a category from a json formated string with the items

orderCatalogNode.prototype.loadItems=function (itemsString) {
	var itemPointer=JSON.parse(itemsString);
	this.items=new Array();
	for (var i in itemPointer.items) {
		this.items[i]=new orderItem(); 
		this.items[i].setFromArray(itemPointer.items[i]);
		this.items[i].parentNode=this;
	}

};

function orderCategory() {
	category.call(this);
}

orderCategory.prototype=Object.create(category.prototype);

orderCategory.prototype.constructor=orderCategory;

function orderItem() {
	catalogItem.call(this);
}

orderItem.prototype=Object.create(catalogItem.prototype);

orderItem.prototype.constructor=orderItem;

orderItem.prototype.mySetView=function(myItemView)  {
		catalogItem.prototype.mySetView.call(this,myItemView);
		myItemElements=this.myView.getElementsByTagName("*");
		for (var j in myItemElements) {
			if (typeof myItemElements[j].getAttribute != 'function') continue;
			if (myItemElements[j].getAttribute("myProperty") == "add") {
				var myItem=new cartItem();
				myItem.setFromArray(this);
				myItemElements[j].myItem=myItem;
				var myPointer=this;
				while (myPointer.parentNode) myPointer=myPointer.parentNode;
				myItem.parentNode=myPointer.myOrderNode;

				myItemElements[j].myItem=myItem;

				myItemElements[j].onclick=function() {
					this.avoidDubleClick=avoidDubleClick;

					this.myItem.parentNode.addItem(this.myItem);
					this.myItem.parentNode.sumPrice();
					showAlertItemAdded(this.myItem);
					blink(this.getAttribute("myFor"));
					this.myItem.parentNode.refreshItemsListView(this.getAttribute("myFor"));
					this.avoidDubleClick();
					
					function avoidDubleClick() {
						this.disabled=true;
							function enableButton(myButton) {
								myButton.disabled=false;
							}		
							setTimeout(enableButton,1200, this);
					}

					function showAlertItemAdded(item) {
						var divElement=document.createElement('div');
						divElement.className="alert";
						var sign='+';
						if (item.quantity<0) sign='';
						var textNode=document.createTextNode(sign+item.quantity+" "+item.name);
						var pElement=document.createElement('p');
						pElement.appendChild(textNode);
						divElement.appendChild(pElement);
						var newAlert=document.body.appendChild(divElement);
						newAlert.onclick=function() {
							this.parentNode.removeChild(this);
						}
						setTimeout(removeAlert,1100,divElement);

						function removeAlert(myAlert) {
							myAlert.parentNode.removeChild(myAlert);
						}
					}
									
					function blink(id) {
						this.element=document.getElementById(id);
						this.blink_time=400;
						this.time_limit=3000;
						this.time_count=0;
						this.blinking=blinking;
						this.blinking();
						function blinking(){
							if (this.element.style.visibility=="visible")
								this.element.style.visibility="hidden";
							else element.style.visibility="visible";
							this.time_count+=this.blink_time;
							if (this.time_count>=this.time_limit) {
								this.element.style.visibility="visible";
								return;
							}
							setTimeout("blinking()",blink_time);
						}
					}
				}
			}
		}
};


/**
 * It sets all form buttons "+ 1 to the cart" for performming the correspondig shopping cart function.
 */
